<!-- 询价单行项目详细信息 -->
<template>
  <el-dialog
    title="详细信息"
    :visible.sync="dialogVisible"
    append-to-body
    width="60%"
  >
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      ref="crud"
      @refresh-change="refreshChange"
    >
      <template slot-scope="{ row }" slot="menu">
        <el-button
          type="text"
          size="mini"
          icon="el-icon-view"
          @click.stop="view(row)"
          v-if="enquiryStatus !== 'toBeQuoted'"
          >查看排名</el-button
        >
        <div v-if="row.adjustFlag !== '1'">
          <el-button
            type="text"
            size="mini"
            icon="el-icon-edit"
            v-if="
              isAdmin &&
              (enquiryStatus === 'toBeConfirmed' || enquiryStatus === 'pending')
            "
            @click.stop="confirm(row)"
            >接受报价</el-button
          >
          <el-button
            type="text"
            size="mini"
            icon="el-icon-edit"
            v-if="
              !isAdmin &&
              row['调整报价'] != '' &&
              (enquiryStatus === 'toBeConfirmed' || enquiryStatus === 'pending')
            "
            @click.stop="confirm(row)"
            >接受报价</el-button
          >
          <el-button
            type="text"
            size="mini"
            icon="el-icon-edit"
            v-if="
              enquiryStatus === 'toBeConfirmed' || enquiryStatus === 'pending'
            "
            @click.stop="edit(row, 2)"
            >修改报价</el-button
          >
          <el-button
            type="text"
            size="mini"
            icon="el-icon-edit"
            v-if="!isAdmin && enquiryStatus === 'toBeQuoted'"
            @click.stop="edit(row, 1)"
            >报价</el-button
          >
        </div>
      </template>
    </avue-crud>
    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">关 闭</el-button>
    </span>
    <rankingInfo ref="rankingInfo" @refresh="refresh()" />
  </el-dialog>
</template>

<script>
import rankingInfo from "./rankingInfo";
import { getItemDetails, quotedPrice, confirmOffer } from "@/api/inquiry/index";
import { mapGetters } from "vuex";

export default {
  data() {
    return {
      dialogVisible: false,
      loading: true,
      data: [],
      inquiryId: "",
      itemId: "",
      itemNo: "",
      enquiryStatus: "", //询价单状态
      option: {
        height: "auto",
        calcHeight: 200,
        tip: false,
        searchShow: false,
        border: true,
        addBtn: false,
        editBtn: false,
        delBtn: false,
        labelWidth: "180",
        menuWidth: "250",
        column: [],
      },
    };
  },
  computed: {
    ...mapGetters(["permission"]),
    //控制承运商账号不显示
    isAdmin() {
      return this.vaildData(this.permission.tms_inquiry_carrier, false);
    },
  },
  components: {
    rankingInfo,
  },
  methods: {
    refreshChange() {
      this.getList();
    },
    open(id, itemId, itemNo, enquiryStatus) {
      this.dialogVisible = true;
      this.inquiryId = id;
      this.itemId = itemId;
      this.itemNo = itemNo;
      this.enquiryStatus = enquiryStatus;
      this.getList();
    },
    //选择返回刷新
    refresh() {
      this.getList();
    },
    getList() {
      this.loading = true;
      getItemDetails(this.itemId).then((res) => {
        const data = res.data.data;
        this.option.column = data.header.map((item) => {
          return {
            label: item,
            prop: item,
          };
        });
        this.data = data.data;
        this.loading = false;
        this.$nextTick(() => {
          this.$refs.crud.$refs.table.doLayout();
        });
      });
    },
    //查看排名
    view(row) {
      this.$refs.rankingInfo.open(row.zrateItem, this.itemNo, this.inquiryId, this.itemId);
    },
    //报价
    edit(row, type) {
      this.$prompt("请输入报价金额", type === 1 ? "报价" : "调整报价", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        inputPattern: /(^[1-9]\d*(\.\d{1,2})?$)|(^0(\.\d{1,2})?$)/,
        inputErrorMessage: "请输入正确的报价金额",
      })
        .then(({ value }) => {
          quotedPrice({
            id: row.id,
            price: value,
          }).then(
            () => {
              this.$message({
                type: "success",
                message: "操作成功!",
              });
              this.refresh();
            },
            (error) => {
              window.console.log(error);
            }
          );
        })
        .catch(() => {});
    },
    //确认报价
    confirm(row) {
      this.$confirm("是否确定接受报价?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          confirmOffer(row.id).then(
            () => {
              this.$message({
                type: "success",
                message: "操作成功!",
              });
              this.refresh();
            },
            (error) => {
              window.console.log(error);
            }
          );
        })
        .catch(() => {});
    },
    //打开选择司机弹框
    addBinding() {
      this.$refs.rankingInfo.open(this.carrierId);
    },
  },
};
</script>

<style></style>
